<?php

namespace AdminV3\Controller;
use Think\Page;


class MovieController extends CommonController{

    public function index()
    {
        $re_method = $_SERVER['REQUEST_METHOD'];
        $map = array();
        if ($re_method == 'GET') {
            $map['name'] = trim(I('get.name'));
            $map['category_id']=trim(I('get.category_id'));
            $map['create_time'] = trim(I('get.create_time'));
            $map['st'] = trim(I('get.st'));
            $map['keyword'] = trim(I('get.keyword'));
            $map['movie_type'] = trim(I('get.movie_type'));
            $map['columns'] = trim(I('get.columns'));
            $map['country_id'] = trim(I('get.country_id'));
        }

        // 用于区分数据状态 1 启用 2 下架 3 删除
        empty($map['st'])?$map['st']=array('in','1,2'):$map['st'];

        $search=$map;

        $p= empty($_GET['p']) ? 0 : $_GET['p'];
        $MovieModel=D('MovieV3');
        $list=$MovieModel->findMovie($map, $p, session('page'));

        foreach($list as $key=>$val){
            $where=array();
            $where['movie_id']= $val['id'];
            $list[$key]['count'] = M('MovieLevel2V3')->where($where)->count();
        }

        $count = $MovieModel->getMovieNum($map);
        $Page = new Page($count,session('page'));
        foreach ($map as $key => $val) {
            $Page->parameter[$key] = urlencode($val);
        }
        $show = $Page->show();
        $this->assign('page', $show);
        $this->assign('list', $list);
        // 栏目分类list

        $this->assign('co',C('COLUMNS'));
        $this->assign('movie_type',get_system_conf('movie_type'));
        $this->assign('country',get_system_conf('country_v3'));

        $this->assign('search',$search);

        //用于跨页面传递 参数。
        session('movie_v3_url_parameters',base64_encode(serialize($_GET)));

        $this->display();
    }

    public function add()
    {
        if (!empty($_POST)) {
            $data['name'] = trim(I('post.name'));
            $data['title'] = trim(I('post.title'));
            $data['country_id'] = trim(I('post.country_id'));
            $data['show_time'] = trim(strtotime(I('post.show_time')));
            $data['play_time'] = trim(strtotime(I('post.show_time')));
            $data['end_time'] = trim(strtotime(I('post.end_time')));
            $data['sort'] = trim(I('post.sort'));
            $data['search_keywords'] = trim(I('post.search_keywords'));
            $data['columns'] = trim(I('post.columns'));
            $data['movie_type'] = trim(I('post.movie_type'));
            $data['collection'] = trim(I('post.collection'));
            //上传图片
            if(!empty ($_FILES)){
                foreach ($_FILES as $name => $value) {
                    if (is_file($value['tmp_name'])) {
                        $this->_uploadImage($data, $name, $value);
                    }
                }
            }

            if(in_array('',$data)){
                $this->error('数据不完整');
            }

            $yetGoodsInfo = M('MovieV3')->getFieldByName($data['name'],'id');
            if(!empty($yetGoodsInfo)){
                $this->error('电影名不能重名');
            }

            $data['create_time'] = time();
            $data['last_update_time'] = time();

            if (M('MovieV3')->add($data)) {
                $this->redirect('Movie/index');
            } else {
                $this->error('添加失败');
            }
        } else {
            $this->assign('co',C('COLUMNS'));
            $this->assign('movie_type',get_system_conf('movie_type'));
            $this->assign('country',get_system_conf('country_v3'));
            $this->display();
        }
    }

    public function edit()
    {
        if (!empty($_GET)) {
            $id = I('id');
            if (!empty($id)) {
                $data=D('MovieV3')->getOneMovie($id);
//                p($data);exit;
                $data['show_time']=date('Y',$data['show_time']);
                $data['play_time']=date('Y-m-d',$data['play_time']);
                $data['end_time']=date('Y-m-d',$data['end_time']);

                $this->assign('data', $data);

                $this->assign('co',C('COLUMNS'));
                $this->assign('country',get_system_conf('country_v3'));
                $this->assign('movie_type',get_system_conf('movie_type'));

                $this->display();
            } else {
                $this->error('非法操作! 没有ID值.');
            }
        }


        if (!empty($_POST)) {
            $data['id'] = trim(I('post.id'));

            if (empty($data['id'])) {
                $this->error('非法操作! 没有ID值.');
            }

            $data['name'] = trim(I('post.name'));
            $re = M('Movie')->where('id !=' . $data['id'] . ' and  name="' . $data['name'].'"')->find();

            if (empty($re)) {

                $data['name'] = trim(I('post.name'));
                $data['title'] = trim(I('post.title'));
                $data['country_id'] = trim(I('post.country_id'));
                $data['show_time'] = trim(strtotime(I('post.show_time')));
                $data['play_time'] = trim(strtotime(I('post.show_time')));
                $data['end_time'] = trim(strtotime(I('post.end_time')));
                $data['sort'] = trim(I('post.sort'));
                $data['search_keywords'] = trim(I('post.search_keywords'));
                $data['columns'] = trim(I('post.columns'));
                $data['movie_type'] = trim(I('post.movie_type'));
                $data['collection'] = trim(I('post.collection'));



                if (in_array('', $data)) {
                    $this->error('信息没有填写完整!');
                }

                //上传图片
                if(!empty ($_FILES)){
                    foreach ($_FILES as $name => $value) {
                        if (is_file($value['tmp_name'])) {
                            $this->_uploadImage($data, $name, $value);
                        }
                    }
                }


                $data['last_update_time'] =time();

                if (M('MovieV3')->save($data) > 0) {
                    $parameters = unserialize(base64_decode(session('movie_v3_url_parameters')));
                    $this->redirect('Movie/index',$parameters);

                } else {
                    $this->error('编辑信息失败!');
                }
            } else {
                $this->error('你输入的名字已经存在');
            }
        }
    }



    //修改商品的状态
    public function alter()
    {
        $idList = I('post.idlist');
        $type = I('post.type');
        $result = array();
        if (!empty($type)) {
            if (!empty($idList)) {
                $idList = rtrim($idList, ',');
                //var_dump($idList);
                $idList = explode(',', $idList);
                //var_dump($idList);
                $data['ok'] = 0;
                $data['error'] = 0;

                foreach ($idList as $k => $val) {
                    //通过审核
                    switch ($type) {
                        case 'on':
                            $newType = 1;
                            break;
                        case 'off':
                            $newType = 2;
                            break;
                        case 'del':
                            $newType = 3;
                            break;
                    }

                    $re = M('MovieV3')->where('id=' . $val)->setField('status', $newType);
                    if ($re > 0) {
                        $data['ok']++;
                    } else {
                        $data['error']++;
                    }
                }
                $result['mes'] = "操作成功:{$data['ok']},失败{$data['error']}";
                $this->ajaxReturn($result);

            } else {
                $result['mes'] = '没有选择的值';
                $this->ajaxReturn($result);
            }
        } else {
            $result['mes'] = '非法操作!';
            $this->ajaxReturn($result);
        }

    }

    public function delList()
    {
        $re_method = $_SERVER['REQUEST_METHOD'];
        $map = array();
        if ($re_method == 'GET') {
            $map['name'] = trim(I('get.name'));
            // $map['title'] = trim(I('get.title'));
            $map['category_id']=trim(I('get.category_id'));
            $map['create_time'] = trim(I('get.create_time'));
            $map['keyword'] = trim(I('get.keyword'));

        }

        // 用于区分数据状态 1 启用 2 下架 3 删除
        $map['st']=3;

        $search=$map;

        $p= empty($_GET['p']) ? 0 : $_GET['p'];
        $MovieModel=D('MovieV3');
        $list=$MovieModel->findMovie($map, $p, session('page'));
        $count = $MovieModel->getMovieNum($map);
        $Page = new Page($count,session('page'));
        foreach ($map as $key => $val) {
            $Page->parameter[$key] = urlencode($val);
        }
        $show = $Page->show();
        $this->assign('page', $show);
        $this->assign('list', $list);

        $this->assign('co',C('COLUMNS'));

        $this->assign('search',$search);

        //用于跨页面传递 参数。
        session('movie_v3_url_parameters',base64_encode(serialize($_GET)));

        $this->display();
    }

    public function getMovieLevel2()
    {
        $movie_id = trim(I('get.movie_id'));
        //var_dump($movie_id);

        if($movie_id){
            $where=array();
            $where['movie_id']=$movie_id;
            $where['status']=1;
            $data =M('MovieLevel2V3')->where($where)->field('id,name,sort')->select();

            //var_dump($data);
            if($data){

                $sortArr=array();
                foreach ($data as $key => $val) {
                    $sortArr[]=$val['sort'];
                }
                array_multisort($data,SORT_ASC,SORT_NUMERIC,$sortArr);

                //var_dump($data);

                $returnData['status']=1;
                $returnData['info']='ok';
                $returnData['data']=$data;
                $this->ajaxReturn($returnData);
            }else{
                $returnData['status']=2;
                $returnData['info']='电影还未添加二级栏目';
                $this->ajaxReturn($returnData);
            }
        }else{
            $returnData['status']=3;
            $returnData['info']='没有选中电影';
            $this->ajaxReturn($returnData);
        }
    }
}